'use strict';

const Controller = require('../../../core/common');

class FeedbacksController extends Controller {

  async index() {
    const { ctx } = this;

    const { type, status } = ctx.request.query;

    const attributes = ['id', 'openId', 'nickname', 'content', 'createdAt', 'updatedAt'];
    const where = {};
    type && Object.assign(where, { type });
    status && Object.assign(where, { status });

    const data = await ctx.service.v1.feedbacks.findAll({ attributes, where });

    this.success(data);
  }

  // 客户端反馈 content
  async create() {
    const { ctx } = this;

    const data = await ctx.service.v1.feedbacks.create(ctx.request.body);

    this.success(data);
  }

  async update() {
    const { ctx } = this;

    const { id = '' } = ctx.params;

    const data = await ctx.service.v1.feedbacks.update({ status: 1 }, {
      where: { id: id.indexOf(',') === -1 ? id : { '$in': id.split(',') } }
    });

    this.success(data);
  }

  async destroy() {
    const { ctx } = this;

    const { id = '' } = ctx.params;

    const data = await ctx.service.v1.feedbacks.destroy({
      where: { id: id.indexOf(',') === -1 ? id : { '$in': id.split(',') } }
    });

    this.success(data);
  }

}

module.exports = FeedbacksController;